package com.game.controller;

import com.game.config.Coefficient;
import com.game.data.BagData;
import com.game.data.MapData;
import com.game.model.*;
import com.game.model.role.Role;
import com.game.model.map.MapPoint;
import com.game.model.role.Monster;
import com.game.view.Print;
import com.game.view.Screen;
import com.game.view.TranslateToChinese;

/**
 * @Description:战斗控制器系统
 **/
public class FightController {
    private Role a;
    private Role b;
    private int count;//回合

    //初始化
    private void init() {
        Screen.clean();
        System.out.println(".....进入战斗....");
    }


    public void fight() {
        init();
        //获取基本属性
        BaseProperty bpa = a.getBaseProperty();
        BaseProperty bpb = b.getBaseProperty();
        SpecialProperty spa = a.getSpecialProperty();
        SpecialProperty spb = b.getSpecialProperty();
        while (true) {
            //暴击提示显示
            boolean aIsCriFlag = false;
            boolean bIsCriFlag = false;
            //a是否死亡
            if (bpa.getHP() <= 0) {
                System.out.println("✘✘✘✘✘✘战斗失败X﹏X✘✘✘✘✘✘");
                Print.back();
                return;
            }
            //a方
            //是否命中
            if (isHit(spa.getHIT(), spb.getAVO())) {
                //攻击
                //是否暴击
                long bleedb = 0;
                if (isCri(spa.getCRI(), spb.getCHR())) {
                    aIsCriFlag = true;
                    bleedb = (int) (bpa.getATK() * Coefficient.CRIP) - bpb.getDEF();
                } else {
                    bleedb = bpa.getATK() - bpb.getDEF();
                }
                //如果攻击力太高导致负数
                if (bleedb < 0) {
                    bleedb = 1l;
                }
                System.out.print("第" + (++count) + "回合：【我】" + (aIsCriFlag ? "触发了暴击" : "") + "对【" + b.getName() + "】造成了" + TranslateToChinese.getFormatLogString("" + bleedb, 34, 0, 0) + "伤害,");
                long hpb = bpb.getHP() - bleedb;
                if (hpb < 0) {
                    bpb.setHP(0);
                } else {
                    bpb.setHP(hpb);
                }
                System.out.println("【" + b.getName() + "】剩余" + TranslateToChinese.getFormatLogString("" + bpb.getHP(), 31, 0, 0) + "血量");
                //b是否死亡
                if (bpb.getHP() <= 0) {
                    System.out.println("★★★★★★战斗胜利o(^▽^)o★★★★★★");
                    //如果是怪物
                    if (b instanceof Monster) {
                        //获取奖励
                        BagData.bag.addToBag(((Monster) b).getRewards());
                        //控制台显示奖励
                        if (((Monster) b).getRewards() != null) {
                            System.out.print("得到了：");
                            ((Monster) b).getRewards().forEach((goods, count) -> {
                                System.out.print("【" + goods.getName() + "】X" + count+" |");
                            });
                        } else {
                            System.out.print("什么都没拿到￣△￣");
                        }
                        //将怪物在地图中移除
                        MapPoint roleInMap = MapController.getRoleInMap(MapData.gameMap);
                        MapController.removeObjectInMap(MapData.gameMap, roleInMap.getX(), roleInMap.getY(), b);
                    }
                    //如果是NPC
                    if (true) {
                        //未开发
                    }
                    Print.back();
                    return;
                }
            } else {
                System.out.println("第" + (++count) + "回合：【" + b.getName() + "】躲避了我的攻击");
            }

            //b方
            //是否命中
            if (isHit(spb.getHIT(), spa.getAVO())) {
                //是否暴击
                long bleeda = 0;
                if (isCri(spb.getCRI(), spa.getCHR())) {
                    bIsCriFlag = true;
                    bleeda = (int) (bpb.getATK() * Coefficient.CRIP) - bpa.getDEF();
                } else {
                    bleeda = bpb.getATK() - bpa.getDEF();
                }

                //如果攻击力太高导致负数
                if (bleeda < 0) {
                    bleeda = 1l;
                }
                System.out.print("第" + (count) + "回合：【" + b.getName() + "】"+(bIsCriFlag?"触发了暴击":"")+"对【我】造成了" + TranslateToChinese.getFormatLogString("" + bleeda, 34, 0, 0) + "伤害,");
                long hpa = bpa.getHP() - bleeda;
                if (hpa < 0) {
                    bpa.setHP(0);
                } else {
                    bpa.setHP(hpa);
                }
                System.out.println("【我】剩余" + TranslateToChinese.getFormatLogString("" + bpa.getHP(), 31, 0, 0) + "血量");
            } else {
                System.out.println("第" + (count) + "回合：我躲避了【" + b.getName() + "】的攻击");
            }
        }
    }

    //是否命中
    public boolean isHit(int hit, int aov) {
        //闪避率=闪避-命中
        int result = aov - hit;
        if (result <= 1) {
            result = 1;
        }
        //如果闪避率大于70 就衰减为每10点加1
        if (result > 70) {
            int k = (result - 70) / 10;
            result = 70 + k;
        }
        //判断概率
        int po = (int) (Math.random() * 100);
        //返回概率
        return po > result;
    }

    //是否暴击
    public boolean isCri(int cri, int chr) {
        //暴击率 = 暴击 - 暴击抵抗
        int result = cri - chr;
        if (result <= 1) {
            result = 1;
        }
        //如果暴击率大于70 就衰减为每10点加1
        if (result > 70) {
            int k = (result - 70) / 10;
            result = 70 + k;
        }
        //判断概率
        int po = (int) (Math.random() * 100);
        //返回概率
        return po < result;
    }

    public FightController(Role a, Role b) {
        this.a = a.deepClone();
        this.b = b.deepClone();
    }
}
